package model;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.CrudRepository;

/**
 * This unit tests shows plain usage of {@link SimpleJpaRepository}.
 * 
 * @author Andre
 */
public class DatentraegerBasicSample {

	private CrudRepository<Datentraeger, Long> datentraegerRepository;
	private EntityManager em;

	/**
	 * Sets up a {@link SimpleJpaRepository} instance.
	 */
	@Before
	public void setUp() {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("filmverwaltung");
		em = factory.createEntityManager();

		datentraegerRepository = new SimpleJpaRepository<Datentraeger, Long>(
				Datentraeger.class, em);

		em.getTransaction().begin();
	}

	@After
	public void tearDown() {

		em.getTransaction().rollback();
	}

	/**
	 * Tests saving datentraegers. Don't mimic transactionality shown here. It
	 * seriously lacks resource cleanup in case of an exception. Simplification
	 * serves descriptivness.
	 */
	@Test
	public void savingDatentraegers() {

		Datentraeger datentraeger = new Datentraeger("Datentraeger");
		datentraeger.setName("Datentraeger");

		datentraeger = datentraegerRepository.save(datentraeger);

		assertEquals(datentraeger,
				datentraegerRepository.findOne(datentraeger.getId()));
	}
}
